﻿using Minecraft.BLL;
using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.Dropinfo;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Cmd
{
	public class ResourceCollect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Resource_ResourceCollect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<ResourceCollectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.resourceItemId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：resourceItemId参数为空）" });
				return;
			}
			string dropId = "";
			Mod_Drawing mod_Drawing = CsvConfig.GetMod_DrawingInfoById(req.resourceItemId);
			if (mod_Drawing != null)
			{
				var drawingResourceTypeIdList = session.GetDrawingResourceTypeIdList();
				if (!drawingResourceTypeIdList.Any(m => m == mod_Drawing.Type_Convert))
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：为【{req.resourceItemId}】的itemId不属于资源采集的范畴）" });
					return;
				}
				dropId = mod_Drawing.Drop;
			}
			else
			{
				Mod_BlockState mod_BlockState = CsvConfig.GetMod_BlockStateInfoById(req.resourceItemId);
				if (mod_BlockState != null)
				{
					dropId = mod_BlockState.Items_drop;
				}
				else
				{
					session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：无法从图纸表或者方块表找到itemId为【{req.resourceItemId}】资源）" });
					return;
				}
			}

			BaseResp baseResp = session.InsertGoodsToBackpackFromDropId(dropId,
				out List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList,
				out List<BackpackCellInfo> posGoodsinfoList,
				out List<KeyValueEnumAttributeInt> dropAttributeList,
				out List<KeyValueEnumAttributeInt> attributeList);

			if (baseResp.RespLevel != RespLevelEnum.Success)
			{
				session.Send(defCommand, req, baseResp);
				return;
			}

			var resp = new ResourceCollectResp
			{
				dropinfoDropResourceInfoList = dropinfoDropResourceInfoList,
				dropAttributeList = dropAttributeList,
				attributeList = attributeList,
				resourceId = req.resourceId,
				cellInfoList = posGoodsinfoList
			};
			session.Send(defCommand, req, resp);
		}
	}
}
